import requests
from bs4 import BeautifulSoup
import time
import csv
from openpyxl import Workbook

# 设置请求头
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}

def get_book_info(url):
    """
    从指定URL获取单页图书信息
    """
    try:
        # 发送请求
        response = requests.get(url, headers=headers)
        response.encoding = response.apparent_encoding
        
        # 解析HTML
        soup = BeautifulSoup(response.text, "html.parser")
        
        # 查找图书列表
        book_list_ul = soup.find("ul", class_="bang_list clearfix bang_list_mode")
        if not book_list_ul:
            print("未找到图书列表")
            return []
        
        books = []
        book_items = book_list_ul.find_all("li")
        
        for item in book_items:
            book_info = {}
            
            # 提取排名
            rank = item.find("div", class_="list_num")
            if rank:
                book_info['rank'] = rank.text.strip()
            
            # 提取书名
            name = item.find("div", class_="name")
            if name:
                book_info['name'] = name.text.strip()
                book_info['url'] = name.a['href']
            
            # 提取作者
            author_divs = item.find_all("div", class_="publisher_info")
            if author_divs:
                author_a = author_divs[0].find("a")
                if author_a:
                    book_info['author'] = author_a.text.strip()
            
            # 提取出版社和出版日期
            if len(author_divs) > 1:
                pub_info = author_divs[1]
                date_span = pub_info.find("span")
                if date_span:
                    book_info['publish_date'] = date_span.text.strip()
                
                pub_a = pub_info.find("a")
                if pub_a:
                    book_info['publisher'] = pub_a.text.strip()
            
            # 提取价格信息
            price = item.find("span", class_="price_n")
            if price:
                book_info['price'] = price.text.strip()
            
            original_price = item.find("span", class_="price_r")
            if original_price:
                book_info['original_price'] = original_price.text.strip()
            
            discount = item.find("span", class_="price_s")
            if discount:
                book_info['discount'] = discount.text.strip()
            
            # 提取评论数
            comments = item.find("div", class_="star")
            if comments:
                comment_a = comments.find("a")
                if comment_a:
                    book_info['comments'] = comment_a.text.strip()
            
            books.append(book_info)
        
        return books
    
    except Exception as e:
        print(f"获取图书信息时出错: {e}")
        return []

def save_to_excel(books, filename="dangdang_bestsellers.xlsx"):
    """
    将图书信息保存为Excel文件
    """
    if not books:
        print("没有图书信息可保存")
        return False
    
    try:
        # 创建一个Workbook对象
        wb = Workbook()
        
        # 获取默认的工作表
        ws = wb.active
        ws.title = "畅销书排行榜"
        
        # 定义表头
        headers = [
            '排名', '书名', '图书链接', '作者', '出版社', 
            '出版日期', '价格', '原价', '折扣', '评论数'
        ]
        
        # 写入表头
        ws.append(headers)
        
        # 定义数据字段顺序（与表头对应）
        fields = [
            'rank', 'name', 'url', 'author', 'publisher', 
            'publish_date', 'price', 'original_price', 'discount', 'comments'
        ]
        
        # 写入数据
        for book in books:
            row_data = [book.get(field, '') for field in fields]
            ws.append(row_data)
        
        # 保存Excel文件
        wb.save(filename)
        
        print(f"\n图书信息已成功保存到 {filename} 文件")
        return True
    
    except Exception as e:
        print(f"保存Excel文件时出错: {e}")
        return False

def main():
    """
    主函数
    """
    # 当当网畅销书排行榜URL模板
    url_template = "http://bang.dangdang.com/books/bestsellers/01.00.00.00.00.00-year-2023-0-1-{page}"
    
    # 设置要抓取的页数
    total_pages = 5
    
    all_books = []
    
    print(f"开始抓取当当网畅销书排行榜，共 {total_pages} 页")
    
    for page in range(1, total_pages + 1):
        url = url_template.format(page=page)
        print(f"\n正在抓取第 {page} 页: {url}")
        
        # 获取当前页的图书信息
        books = get_book_info(url)
        
        if books:
            print(f"第 {page} 页成功抓取 {len(books)} 本图书")
            all_books.extend(books)
        else:
            print(f"第 {page} 页未抓取到任何图书信息")
        
        # 添加延迟，避免给服务器造成过大压力
        if page < total_pages:
            print("等待2秒后继续抓取下一页...")
            time.sleep(2)
    
    print(f"\n抓取完成，共获取 {len(all_books)} 本图书信息")
    
    # 打印前5本书的信息作为示例
    if all_books:
        print("\n前5本书信息示例:")
        for i, book in enumerate(all_books[:5]):
            print(f"\n{book['rank']} {book['name']}")
            print(f"  作者: {book.get('author', 'N/A')}")
            print(f"  出版社: {book.get('publisher', 'N/A')}")
            print(f"  价格: {book.get('price', 'N/A')} (原价: {book.get('original_price', 'N/A')})")
    
    # 保存数据到Excel文件
    save_to_excel(all_books)
    
    return all_books

if __name__ == "__main__":
    main()